package com.choosefine.it.worker.controller;

import com.choosefine.it.worker.model.vo.FindWorkVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.HttpServiceBean;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.basedata.model.UserInfo;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.common.utils.URLHandlerUtils;
import com.choosefine.it.worker.model.FindWork;
import com.choosefine.it.worker.model.UpdateStatusDTO;
import com.choosefine.it.worker.service.FindWorkService;
import com.github.pagehelper.Page;

@RestController
/**
 * 11.9dzq 工人找工作
 * @author Administrator
 *
 */
@Api(value = "班组找工人、工人角色-发布找工作编辑-删除-详情功能", description = "廖仉根|董自齐")
@RequestMapping("/findWork")
public class FindWorkController extends BaseController {
	@Autowired
	private FindWorkService findWorkService;

	@Autowired
	private HttpServiceBean httpServiceBean;


	@Value("#{yaml.get('basedata.userinfo.serverGet')}")
	final String userinfoURL = null;

	/**
	 * 11.9dzq 获取工人发布找工作的信息
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ApiOperation(value = "输入条件查看工人发布找工作信息",response=FindWork.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "workAreaIds", value = "工作地区id", dataType = "String", required = false, paramType = "query"),
			@ApiImplicitParam(name = "workTypeId", value = "工种id", dataType = "String", required = false, paramType = "query"),
			@ApiImplicitParam(name = "pageNum", value = "起始页", dataType = "int", required = true, paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页行", dataType = "int", required = true, paramType = "query") })
	@ApiResponses({
			@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
			@ApiResponse(code = ResultCode.FAILED, message = "请求参数错误") })
	public String getFindWork(
			@RequestParam(name = "workAreaIds",required = false) String workAreaIds,// 地区id,11.15dzq
			@RequestParam(name = "workTypeId",required = false)  String workTypeId,// 工种id,11.15dzq
			PageDTO page) throws Exception {

		String result = null;
		Map<String, Object> param = new HashMap<String, Object>();// 插入分页数据
		param.put("workareas", workAreaIds);// 将期望工作的地区拆开为集合
		param.put("worktype", workTypeId);
		Page<FindWork> list = findWorkService.selectByRegionOrType(param, page);
		if (list.size() > 0) {
			result = responseSuccess(list);
		} else {
			result = responseFail(ResultCode.MSG_DATA_ISEMPTY);
		}
		return result;
	}

	/**
	 * 11.10dzq 根据id查看发布人的用户信息
	 */
	@RequestMapping(value = "/find/{userCode}", method = RequestMethod.GET)
	@ApiOperation(value = "根据id查看发布人的用户信息",response=UserInfo.class)
	@ApiImplicitParam(name = "userCode", value = "用户code", dataType = "String", required = true, defaultValue = "", paramType = "path")
	@ApiResponses({
		@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "成功返回结果"),
		@ApiResponse(code = ResultCode.FAILED, message = "系统错误，无数据") })
	public String getOneFindWork(
		@PathVariable("userCode")  String userCode) throws Exception { //主键id
		if(StringUtils.isEmpty(userCode)){//如果数据为空，则返回错误
			return responseFail(CommonConstant.PARAMETER_ERROR);
		}
		StringBuffer url=URLHandlerUtils.getUrlBuffer(shixiaobaoURL,  userinfoURL).append("/").append(userCode);
		String result=httpServiceBean.getRequestBody(url.toString(), null);//工人的身份信息
		//待确定是否增加工人的工作历时信息11.23dzq
		return result;
	}

	/**
	 * pqq
	 * @param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="getFindWorkById/{userCode}/{uuid}/{workerPulishId}",method=RequestMethod.GET)
	@ApiOperation(value = "查看工人发布找工作信息详情yuxin",response=FindWorkVO.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userCode", value = "班组的code", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "uuid",value = "uuid",required = true,dataType = "String",paramType = "path"),
			@ApiImplicitParam(name = "workerPulishId", value = "工人发布找工作的id", required = true, dataType = "Integer", paramType = "path")
	})
	public String getFindWorkById(@PathVariable String userCode, // 用户id
								  @PathVariable String uuid,
								  @PathVariable Integer workerPulishId) throws Exception {
		Map<String, Object> map = new HashMap<>();
		map.put("userCode",userCode);
		map.put("uuid",uuid);
		map.put("publishId",workerPulishId);
		map.put("type",CommonConstant.TEAM_COLLECT_WORKER);
		FindWorkVO mp=findWorkService.selectByPrimaryKey(map);//查询数据
		if (mp == null) {// 如果数据为空，则返回错误
			return responseFail("系统错误，无数据");
		}
		return responseSuccess(mp);
	}
	
	/**
	 * 工人我的-发布管理-我的发布-发布一览表-发布找工作编辑  - No. 3
	 * 
	 * @param
	 * @throws BusinessException 
	 */
	@ApiOperation("工人我的-发布管理-我的发布-发布一览表-发布找工作编辑(廖仉根) ")
	@RequestMapping(method = RequestMethod.POST, value = "/modify/publish", produces = MediaType.APPLICATION_JSON_VALUE)
	public String modifyPublishWork(@RequestBody FindWork findWork) throws BusinessException {
		String result = null;
		int num = findWorkService.updatePublishWorkInfo(findWork);
		result = num > 0 ? responseSuccess() : responseFail();
		return result;
	}
	
	/**
	 * 工人我的-发布管理-我的发布-发布一览表-发布找工作-删除 - No. 4
	 * 
	 * @param
	 */
	@ApiOperation("工人我的-发布管理-我的发布-发布一览表-发布找工作删除 (廖仉根)")
	@RequestMapping(method = RequestMethod.PUT, value = "/delete/publish", produces = MediaType.APPLICATION_JSON_VALUE)
	public String deletePublishWork(@RequestBody UpdateStatusDTO updateStatusDTO) {
		String result = null;
		Map<String,Object> map = new HashMap<>();
		if(updateStatusDTO.getIsValid() != null
				&& (updateStatusDTO.getIsValid().equals(0) || updateStatusDTO.getIsValid().equals(1))){
			map.put("isValid", updateStatusDTO.getIsValid());
		}
		if(updateStatusDTO.getPublishStatus() != null
				&& (updateStatusDTO.getPublishStatus().equals(0) || updateStatusDTO.getPublishStatus().equals(1))){
			map.put("publishStatus", updateStatusDTO.getPublishStatus());
		}
		if(!map.isEmpty()){
			map.put("id", updateStatusDTO.getId());
			int num = findWorkService.updateStatusById(map);
			result = num > 0 ? responseSuccess() : responseFail("Has been deleted !");
		}else{
			result = responseFail("Has been deleted !");
		}
		return result;
	}
	
	/**
	 * 工人我的-发布管理-我的发布-发布一览表-发布找工作 明细 - No. 5
	 * 
	 * @param
	 */
	@ApiOperation("工人我的-发布管理-我的发布-发布一览表-发布找工作 明细(廖仉根)")
	@RequestMapping(method = RequestMethod.GET, value = "/publish/detail/{publishId}", produces = MediaType.APPLICATION_JSON_VALUE)
	public String queryPublishWorkDetail(@PathVariable Integer publishId) {
		String result = null;
		FindWork findWork = findWorkService.queryPublishWorkDetail(publishId);
		result = null != findWork ? responseSuccess(findWork) : responseFail();
		return result;
	}
}